<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Data.mysql.php');
require_once($Config->WebCMSRoot.'Data/Image.php');

Class SlideshowData extends db
{
	function SlideshowData()
	{
		parent::db();
		
		$this->tableName = 'slideshow';
		
		$this->AddMap("ID", "id", "id", DATATYPE_INTEGER, true);
		$this->AddMap("Key", "itemKey", "itemKey", DATATYPE_TEXT, true);
		$this->AddMap("Description", "description", "description", DATATYPE_TEXT, false);
		$this->AddMap("NavigationID", "navigation_id", "navigation_id", DATATYPE_TEXT, true);
		/*$this->AddMap("Title", "title", "title", DATATYPE_TEXT, false);
		$this->AddMap("Summary", "summary", "summary", DATATYPE_TEXT, false);
		$this->AddMap("DateAdded", "dateAdded", "dateAdded", DATATYPE_DATETIME, true);
		$this->AddMap("SortOrder", "sortOrder", "sortOrder", DATATYPE_INTEGER, false);*/
	}
  
	function Retrieve(&$slideshow, $limit=null, $links=null)
	{
//print('<pre>' . var_export($slideshow, true) . '</pre>');//exit;
		$sqlJoin = '';
		//$sqlWhere = $this->GetWhereClause($slideshow);

		$sqlWhere = 'AND tbl.id = ci.slideshow_id ';
		if (isset($slideshow->Parameters["navigation"]) && count($slideshow->Parameters["navigation"]))
		{
			$sqlJoin .= ', WebCMS_navigation n ';
			$sqlWhere .= 'AND tbl.navigation_id = n.id ';
			$sqlWhere .= 'AND p.file = \'' . $slideshow->Parameters["page"][0] . '\'';
		}
		/*if (isset($links) && count($links) > 0)
		{
			//$sqlJoin  = ', slideshow_navigation cn ';
			//$sqlJoin .= ', WebCMS_navigation n ';
			$sqlWhere .= 'AND cn.slideshow_id = tbl.id ';
			$sqlWhere .= 'AND cn.navigation_id = n.id ';
			//if ($sqlWhere != '')
			//{
			//	$sqlWhere = 'AND ' . substr($sqlWhere, 6);
			//}
			$sqlWhere .= 'AND (';
			foreach ($links as $key=>$val)
			{
				$sqlWhere .= "n.link LIKE '%" . $val . "' OR ";
			}
			$sqlWhere = 'WHERE ' . substr($sqlWhere, 4, -4) . ') ';
		}*/
		if (strlen($sqlWhere) > 0)
		{
			$sqlWhere = 'WHERE ' . substr($sqlWhere, 4) . ' ';
		}
		
		$sql  = "SELECT DISTINCT ";
		$sql .= $this->GetFieldList();
		$sql .= "FROM " . $this->GetTableName();
		$sql .= ', WebCMS_slideshow_images ci ';
		$sql .= $sqlJoin;
		$sql .= $sqlWhere;
		//$sql .= "ORDER BY sortOrder, title ";
		if (isset($limit))
		{
			$sql .= "LIMIT 0, $limit ";
		}

		$slideshow->Collection = array();

		$rows = $this->Select($sql);
//print(count($rows));
//print('<pre>' . var_export($rows, true) . '</pre>');exit;

		foreach($rows as $key=>$row)
		{
			//$slideshow->Collection[] = $this->FillRow($row);
			$car = $this->FillRow($row);
//print('<pre>' . var_export($car, true) . '</pre>');exit;
			$car->RetrieveImages(&$car);
//print('<pre>' . var_export($car, true) . '</pre>');//exit;

			$slideshow->Collection[] = $car;
		}
//print($sql);//exit;
//print('<pre>' . var_export($slideshow->Collection, true) . '</pre>');exit;
	}
  
	function RetrieveImages(&$slideshow)
	{
		$sql  = "SELECT images_id id ";
		$sql .= "FROM webcms_slideshow_images ";
		$sql .= "WHERE slideshow_id = " . $slideshow->ID . " ";
		$sql .= "ORDER BY sortOrder ";

		$images = array();
		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$images[] = $row['id'];
		}
		
		return $images;
		/*$sql  = "SELECT images_id id ";
		$sql .= "FROM webcms_slideshow_images ci, webcms_images i ";
		$sql .= "WHERE slideshow_id = " . $slideshow->ID . " ";
		$sql .= "AND ci.images_id = i.id ";
		$sql .= "ORDER BY sortOrder ";

		$images = array();
		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$images[] = $row['id'];
		}
		
		return $images;*/
	}
  
	/*function RetrieveNavigationLinks(&$slideshow)
	{
		$sql  = "SELECT navigation_id id ";
		$sql .= "FROM slideshow_navigation ";
		$sql .= "WHERE slideshow_id = " . $slideshow->ID . " ";

		$navigation = array();
		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$navigation[] = $row['id'];
		}
		
		return $navigation;
	}*/
  
	function Update(&$slideshow)
	{
		//$slideshow->Key = $this->GenerateSafeString($slideshow->Title);
		
		if (isset($slideshow->ID) && !empty($slideshow->ID) && $slideshow->ID > 0)
		{
			$sql  = "UPDATE slideshow ";
			$sql .= $this->GetUpdateList($slideshow);
			$sql .= "WHERE id=" . $slideshow->ID;
		}
		else
		{
			$sql  = "INSERT INTO slideshow ";
			$sql .= $this->GetInsertFieldList();
			$sql .= $this->GetInsertValueList(&$slideshow);
		}

	    $rows = $this->Execute($sql);
	}
  
	function Delete(&$slideshow)
	{
		// Images
		$sql  = "DELETE FROM slideshow_images ";
		$sql .= "WHERE slideshow_id=" . $slideshow->ID;

	    $rows = $this->Execute($sql);
		
		// Item
		$sql  = "DELETE FROM slideshow ";
		$sql .= "WHERE id=" . $slideshow->ID;

	    $rows = $this->Execute($sql);
	}
  
	function DeleteImages(&$slideshow)
	{
		// Images
		foreach($slideshow->Images as $key=>$image)
		{
			$sql  = "DELETE FROM slideshow_images ";
			$sql .= "WHERE slideshow_id=" . $slideshow->ID . " ";
			$sql .= "AND images_id=" . $image->ID . " ";

		    $rows = $this->Execute($sql);
		}
	}
  
	function DeleteNavigation(&$slideshow)
	{
		$sql  = "DELETE FROM slideshow_navigation ";
		$sql .= "WHERE slideshow_id=" . $slideshow->ID . " ";

		$rows = $this->Execute($sql);
	}
  
	function AddImage(&$slideshow, &$image)
	{
		// Images
		$sql  = "INSERT INTO slideshow_images ";
		$sql .= "(slideshow_id, images_id) ";
		$sql .= "VALUES ";
		$sql .= "(".$slideshow->ID.", ".$image->ID.") ";

		$result = $this->Execute($sql);
	}
  
	function AddNavigation(&$slideshow, $navigation)
	{
		if (count($navigation) > 0)
		{
			$sql  = "INSERT INTO slideshow_navigation ";
			$sql .= "(slideshow_id, navigation_id) ";
			$sql .= "VALUES ";
			foreach ($navigation as $key=>$nav)
			{
				$sql .= "(".$slideshow->ID.", ".$nav."), ";
			}
			$sql = substr($sql, 0, -2);

			$result = $this->Execute($sql);
		}
	}
  
	function FillRow($row)
	{
		$slideshow = new SlideshowModel();
		
		parent::FillRow(&$slideshow, &$row);
//print('<pre>' . var_export($row, true) . '</pre>');exit;
		
		return $slideshow;
	}

	function GetFieldList()
	{
		parent::GetFieldList(&$select);
		
		return $select;
	}
}
?>

